import React from 'react'
import PropTypes from 'prop-types'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import Approve from '@components/ApprovePre/Index'
import SecretaryInput from './Preservation/SecretaryInput'
import SecretaryHistory from './Preservation/SecretaryHistory'
import StepFourInput from './Preservation/StepFourInput'
import StepFourHistory from './Preservation/StepFourHistory'
import { awaitWrap } from '@js/tool'
import { requestFiles } from '@js/request'
import { message } from 'antd'

class Preservation extends Component {
  dealBaoQuanFormData01 (data) {
    const vars = data.vars || {}
    vars.preservation_court = vars.preservation_court && vars.preservation_court.replace('，', ',')
    vars.preservation_partyid = vars.preservation_partyid.join(',')
    vars.preservation_party = vars.preservation_party.join(',')
    vars.delay_day = Number(vars.delay_day) === 0 ? '' : vars.delay_day
    return Promise.resolve(data)
  }

  async dealBaoQuaoFormData04 (data) {
    let fileid = ''
    const file = data.file ? data.file.fileList : []
    if (file.length) {
      const hide = message.loading('文件上传中', 120)
      const [e, d] = await awaitWrap(requestFiles(file))
      hide()
      if (e) {
        return message.warning(e.message)
      }
      fileid = d.map(li => li.id).join()
    }
    delete data.file
    data.vars.target_fileid = fileid
    return data
  }

  // 获取表单初始化数据
  getFormInitialData (instance) {
    switch (instance.currentStep.step_key) {
      case 'baoquan01':
        return { vars: { delay_day: 0 } }
      default:
        return {}
    }
  }

  // 呈批表单格式化数据
  beforeSubmit (instance, data) {
    switch (instance.currentStep.step_key) {
      case 'baoquan01':
        return this.dealBaoQuanFormData01(data)
      case 'baoquan04':
        return this.dealBaoQuaoFormData04(data)
      default:
        return data
    }
  }

  // 格式化当前数据
  formatCurrentStepData (currentStep, instance) {
    const isFinance = this.props.caseDetail.casetype === '金融案件'
    if (currentStep.step_key === 'baoquan01') {
      currentStep.actions.map(li => {
        if (isFinance && li.opt_add !== 'disagree') {
          li.level = 1
        }
      })
    }
    return currentStep
  }

  // 渲染当前操作步骤线上部分
  renderCurrentStepLineTop (formRef, instance, renderFn) {
    switch (instance.currentStep.step_key) {
      case 'baoquan01':
        return <SecretaryInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} />
      case 'baoquan04':
        return <StepFourInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} />
      default:
        return renderFn()
    }
  }

  // 渲染历史
  renderHistory (action, renderFn, instance) {
    const key = action.step_key
    // const isDisagree = action.opt_add === 'disagree'
    if (key === 'baoquan01') {
      return <SecretaryHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} />
    } else if (key === 'baoquan04') {
      return <StepFourHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} />
    }
    return renderFn(action)
  }

  render () {
    return (
      <div>
        <Approve
          caseDetail={this.props.caseDetail}
          flowKey='baoquan'
          afterAction={(instance, action) => this.props.refreshCaseData()}
          creatable={this.props.editable}
          renderCurrentStepLineTop={(...params) => this.renderCurrentStepLineTop(...params)}
          renderHistory={(h, renderFn, instance) => this.renderHistory(h, renderFn, instance)}
          initialCurrentStepFormData={instance => this.getFormInitialData(instance)}
          beforeSubmit={(...params) => this.beforeSubmit(...params)}
          formatCurrentStepData={(currentStep, instance) => this.formatCurrentStepData(currentStep, instance)}
          multiply
        />
      </div>
    )
  }
}

Preservation.propTypes = {
  caseDetail: PropTypes.object,
  // active: PropTypes.bool,
  // refreshCaseData: PropTypes.func
  editable: PropTypes.bool
  // fileList: PropTypes.array
  // getFileList: PropTypes.func
}
Preservation.defaultProps = {
  caseDetail: {},
  active: false,
  refreshCaseData: () => {},
  editable: false,
  fileList: [],
  getFileList: () => {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(Preservation)
